package routes

import (
	"github.com/gorilla/mux"
	"goblog/app/http/controllers"
	"goblog/app/http/middlewares"
	"net/http"
)

// RegisterWebRoutes ： Web 路由注册
func RegisterWebRoutes(router *mux.Router) {

	// 静态页面
	pc := new(controllers.PagesController)
	router.NotFoundHandler = http.HandlerFunc(pc.NotFound)

	router.HandleFunc("/about", pc.About).Methods("GET").Name("about")

	// 文章相关页面
	ac := new(controllers.ArticlesController)
	router.HandleFunc("/", ac.Index).Methods("GET").Name("home")
	router.HandleFunc("/articles", ac.Index).Methods("GET").Name("articles.index")

	router.HandleFunc("/articles/create", middlewares.Auth(ac.Create)).Methods("GET").Name("articles.create")
	router.HandleFunc("/articles", middlewares.Auth(ac.Store)).Methods("POST").Name("articles.store")
	router.HandleFunc("/articles/{id:[0-9]+}", ac.Show).Methods("GET").Name("articles.show")
	router.HandleFunc("/articles/{id:[0-9]+}/edit", middlewares.Auth(ac.Edit)).Methods("GET").Name("articles.edit")
	router.HandleFunc("/articles/{id:[0-9]+}", middlewares.Auth(ac.Update)).Methods("POST").Name("articles.update")
	router.HandleFunc("/articles/{id:[0-9]+}/delete", middlewares.Auth(ac.Delete)).Methods("POST").Name("articles.delete")

	// 文章分类
	cc := new(controllers.CategoriesController)
	router.HandleFunc("/categories/create", middlewares.Auth(cc.Create)).Methods("GET").Name("categories.create")
	router.HandleFunc("/categories", middlewares.Auth(cc.Store)).Methods("POST").Name("categories.store")
	router.HandleFunc("/categories/{id:[0-9]+}", middlewares.Auth(cc.Show)).Methods("GET").Name("categories.show")

	// 用户认证
	auc := new(controllers.AuthController)
	router.HandleFunc("/auth/register", middlewares.Guest(auc.Register)).Methods("GET").Name("auth.register")
	router.HandleFunc("/auth/do-register", middlewares.Guest(auc.DoRegister)).Methods("POST").Name("auth.doregister")
	router.HandleFunc("/auth/login", middlewares.Guest(auc.Login)).Methods("GET").Name("auth.login")
	router.HandleFunc("/auth/do-login", middlewares.Guest(auc.DoLogin)).Methods("POST").Name("auth.dologin")
	router.HandleFunc("/auth/logout", middlewares.Auth(auc.Logout)).Methods("POST").Name("auth.logout")
	// 发送邮件，找回密码
	router.HandleFunc("/auth/sendemail", auc.SendEmail).Methods("GET").Name("auth.sendemail")
	router.HandleFunc("/auth/do-sendemail", auc.DoSendEmail).Methods("POST").Name("auth.dosendemail")
	// 重置密码
	router.HandleFunc("/auth/reset/{id:[0-9]+}", auc.ResetPassword).Methods("GET").Name("auth.reset")
	router.HandleFunc("/auth/doreset", auc.DoResetPassword).Methods("POST").Name("auth.doreset")


	// 用户相关
	uc := new(controllers.UserContoller)
	router.HandleFunc("/users/{id:[0-9]+}", uc.Show).Methods("GET").Name("users.show")
	// 静态资源
	router.PathPrefix("/css/").Handler(http.FileServer(http.Dir("./public")))
	router.PathPrefix("/js/").Handler(http.FileServer(http.Dir("./public")))

	// -----  全局中间件 --------

	// 开启会话
	router.Use(middlewares.StartSession)
}

// Api 的路由
func RegisterAPIRoutes(router *mux.Router) {

}
